package com.example.android_entrainement;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jim on 21/07/13.
 */
public class CardListViewAdapter extends ArrayAdapter<CardEntity> {

    private List<CardEntity> listData = new ArrayList<CardEntity>();
    private Context context;
    private LayoutInflater layoutInflater;

    private TextView cardName;
    private TextView cardRarity;
    private TextView cardCapa;
    private WebImage cardImage ;

    public CardListViewAdapter(Context context, int textViewResourceId,
                               List<CardEntity> objects)

    {
        super(context, textViewResourceId, objects);
        this.context = context;
        this.listData = objects;
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public CardEntity getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        if (row == null) {
           LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           row = inflater.inflate(R.layout.cards_list_row,parent,false);
        }

        CardEntity cards = getItem(position);

        cardName = (TextView) row.findViewById(R.id.text_card_name);
        cardRarity = (TextView) row.findViewById(R.id.text_card_rarity);
        cardCapa = (TextView) row.findViewById(R.id.text_card_capa);
        cardImage = (WebImage) row.findViewById(R.id.card_imageView);


        cardName.setText(cards.getName());
        cardRarity.setText(cards.getRarity());
        cardCapa.setText(cards.getCapacity());
        cardImage.startLoading("http://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/300px-PNG_transparency_demonstration_1.png");
       //cardImage.startLoading("https://dl.dropboxusercontent.com/u/36356344/"+cards.getImageResPath()+".png");


        return row;
    }
}
